package com.qqtop10.manager.service.impl;

import java.lang.reflect.Method;

import org.springframework.ui.ModelMap;

public class BaseService {
	public ModelMap asMap(Object... objects) {
		ModelMap map = new ModelMap();
		if (objects == null || objects.length == 0) {
			return map;
		}
		for (int i = 0; i < objects.length; i++) {
			if (objects[i] == null) {
				continue;
			}
			Class<?> type = objects[i].getClass();
			Method[] methods = type.getMethods();
			for (int j = 0; j < methods.length; j++) {
				String mrthodName = methods[j].getName();
				if (mrthodName.length() > 3 && mrthodName.charAt(0) == 'g' && mrthodName.charAt(1) == 'e'
						&& mrthodName.charAt(2) == 't') {
					String fieldName = Character.toLowerCase(mrthodName.charAt(3)) + mrthodName.substring(4);
					Object value;
					methods[j].setAccessible(true);
					try {
						value = methods[j].invoke(objects[i]);
					} catch (Exception e) {
						value = null;
					}
					if (value != null) {
						map.put(fieldName, value);
					}
				}
			}
		}
		return map;
	}
}
